//=============================================================================
#include "CEntity.h"

//=============================================================================
std::vector<CEntity> CEntity::EntityList;

CTexture CEntity::Tex_Test;

//=============================================================================
CEntity::CEntity() {
    Width   = 32;
    Height  = 64;

    Col_X       = 3;
    Col_Y       = 38;
    Col_Width   = 25;
    Col_Height  = 24;

    CurrentFrameCol = 1;
    CurrentFrameRow = 1;

    FrameCols = 4;
    FrameRows = 2;

    NumFrames = 0;
}

//=============================================================================
bool CEntity::OnLoad(char* File) {
    if(Tex_Sprite.OnLoad(File) == false) {
        return false;
    }

    return true;
}

//-----------------------------------------------------------------------------
void CEntity::OnLoop() {
}

//-----------------------------------------------------------------------------
void CEntity::OnRender() {
    float tX = 0;
    float tY = 0;

    float tW = CurrentFrameCol / (float)FrameCols;
    float tH = CurrentFrameRow / (float)FrameRows;

    Tex_Sprite.Bind();

    glBegin(GL_QUADS);
        glTexCoord2f(tX,        tY);        glVertex3f(X, Y, 0);
        glTexCoord2f(tX + tW,   tY);        glVertex3f(X + Width, Y, 0);
        glTexCoord2f(tX + tW,   tY + tH);   glVertex3f(X + Width, Y + Height, 0);
        glTexCoord2f(tX,        tY + tH);   glVertex3f(X, Y + Height, 0);
    glEnd();
}

//------------------------------------------------------------------------------
void CEntity::OnCleanup() {
}

//==============================================================================
void CEntity::OnMove(int SpeedX, int SpeedY) {
    if(SpeedX == 0 && SpeedY == 0) {
        return;
    }

    float NewX = (SpeedX * CFPS::FPSControl.GetSpeedFactor()) + X;
    float NewY = (SpeedY * CFPS::FPSControl.GetSpeedFactor()) + Y;

    if(NewX < 0) NewX = X;
    if(NewY < 0) NewY = Y;

    int tX = (NewX + Col_X) / TILE_SIZE;
    int tY = (NewY + Col_Y) / TILE_SIZE;

    int tX2 = ((NewX + Col_X) + Col_Width)  / TILE_SIZE;
    int tY2 = ((NewY + Col_Y) + Col_Height) / TILE_SIZE;

    //Moving Left/Right
    if(SpeedX != 0) {
        bool MoveX = true;

        //Moving Right
        if(SpeedX > 0) {
            int cX = tX2;
            for(int cY = tY;cY <= tY2;cY++) {
                int MapID = CArea::AreaControl.GetMapID(cX * 16, cY * 16);
                int TileID = CArea::AreaControl.GetTileID(cX * 16, cY * 16);

                if(CArea::AreaControl.MapList[MapID].TileList[TileID].TypeID == TILE_TYPE_BLOCK) {
                    MoveX = false;
                }

                //Tex_Test.RenderQuad(cX * 16, cY * 16, 16, 16);
            }
        //Moving Left
        }else{
            int cX = tX;
            for(int cY = tY;cY <= tY2;cY++) {
                int MapID = CArea::AreaControl.GetMapID(cX * 16, cY * 16);
                int TileID = CArea::AreaControl.GetTileID(cX * 16, cY * 16);

                if(CArea::AreaControl.MapList[MapID].TileList[TileID].TypeID == TILE_TYPE_BLOCK) {
                    MoveX = false;
                }

                //Tex_Test.RenderQuad(cX * 16, cY * 16, 16, 16);
            }
        }

        if(MoveX) {
            X = NewX;
        }
    }

    //Moving Up/Down
    if(SpeedY != 0) {
        bool MoveY = true;

        //Moving Down
        if(SpeedY > 0) {
            int cY = tY2;
            for(int cX = tX;cX <= tX2;cX++) {
                int MapID = CArea::AreaControl.GetMapID(cX * 16, cY * 16);
                int TileID = CArea::AreaControl.GetTileID(cX * 16, cY * 16);

                if(CArea::AreaControl.MapList[MapID].TileList[TileID].TypeID == TILE_TYPE_BLOCK) {
                    MoveY = false;
                }

                //Tex_Test.RenderQuad(cX * 16, cY * 16, 16, 16);
            }
        //Moving Up
        }else{
            int cY = tY;
            for(int cX = tX;cX <= tX2;cX++) {
                int MapID = CArea::AreaControl.GetMapID(cX * 16, cY * 16);
                int TileID = CArea::AreaControl.GetTileID(cX * 16, cY * 16);

                if(CArea::AreaControl.MapList[MapID].TileList[TileID].TypeID == TILE_TYPE_BLOCK) {
                    MoveY = false;
                }

                //Tex_Test.RenderQuad(cX * 16, cY * 16, 16, 16);
            }
        }

        if(MoveY) {
            Y = NewY;
        }
    }
}

//=============================================================================
bool CEntity::Collides(int X, int Y, int Width, int Height) {
    if(this->X + Col_X >= X && this->X + Col_X + Col_Width <= X + Width &&
        this->Y + Col_Y >= Y && this->Y + Col_Y + Col_Height <= X + Height) {
        return true;
    }

    return false;
}

//=============================================================================
